package com.haojie.codec;

import com.haojie.common.RPCRequest;
import com.haojie.common.RPCResponse;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import lombok.AllArgsConstructor;

@AllArgsConstructor
public class MyEncoder extends MessageToByteEncoder {
    Serializer serializer;
    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, Object o, ByteBuf byteBuf) throws Exception {
        System.out.println(o.getClass());

        //判断写入的消息类型 request和response
        if(o instanceof RPCRequest) {
            byteBuf.writeShort(MessageType.REQUEST.getCode());
        }
        else if(o instanceof RPCResponse) {
            byteBuf.writeShort(MessageType.RESPONSE.getCode());
        }

        //写入序列化方式
        byteBuf.writeShort(serializer.getType());
        //得到序列化数组
        byte[] serialize = serializer.serialize(o);

        //写入长度
        byteBuf.writeInt(serialize.length);
        //写入序列化字节数组
        byteBuf.writeBytes(serialize);
    }
}
